package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcHospitalEntity;
import com.dhcc.cdc.busi.service.ICdcHospitalService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 院区
 * 
 * @ClassName: CdcHospitalController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdchospital")
public class CdcHospitalController extends BaseController {

	private String PREFIX = "/backend/cdc/cdchospital/";

	@Autowired
	private ICdcHospitalService cdcHospitalService;
	

    /**
     * 跳转到院区列表首页
     */
    @RequestMapping("")
    @Permission("cdchospital:list")
    public String index() {
        return PREFIX + "cdchospital.html";
    }
	
    /**
     * 跳转到新增院区
     */
    @RequestMapping("/to_add")
    @Permission("cdchospital:add")
    public String toAdd() {
        return PREFIX + "cdchospital_add.html";
    }
	
	/**
     * 跳转到修改院区
     */
    @RequestMapping("/to_update/{cdcHospitalId}")
    @Permission("cdchospital:update")
    public String toUpdate(@PathVariable Long cdcHospitalId, Model model) {
        CdcHospitalEntity cdcHospital = this.cdcHospitalService.selectById(cdcHospitalId);
        model.addAttribute("cdcHospital",cdcHospital);
        return PREFIX + "cdchospital_edit.html";
    }

    /**
     * 获取院区列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdchospital:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcHospitalEntity> page = new PageFactory<CdcHospitalEntity>().defaultPage();
        List<CdcHospitalEntity> result = this.cdcHospitalService.page(page,params);
        page.setRecords((List<CdcHospitalEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    @Permission("cdchospital:list")
    public Object query(@RequestParam Map<String, Object> params) {
        List<CdcHospitalEntity> result = this.cdcHospitalService.list(params);
        return result;
    }

    /**
     * 新增院区
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdchospital:add")
    @BussinessLog(value = "新增院区")
    public Object add(CdcHospitalEntity cdcHospital) {
        if (CommonUtil.isOneEmpty(cdcHospital)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcHospital.insert();
        return R.ok();
    }

    /**
     * 删除院区
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdchospital:delete")
    @BussinessLog(value = "删除院区")
    public Object delete(@RequestParam Long cdcHospitalId) {
	
        this.cdcHospitalService.deleteById(cdcHospitalId);

        return R.ok();
    }

    /**
     * 修改院区
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdchospital:update")
    @BussinessLog(value = "修改院区")
    public Object update(CdcHospitalEntity cdcHospital) {
        if (CommonUtil.isOneEmpty(cdcHospital)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcHospitalEntity old = this.cdcHospitalService.selectById(cdcHospital.getId());

        old.setXcode(cdcHospital.getXcode());
        old.setCode(cdcHospital.getCode());
        old.setDescription(cdcHospital.getDescription());
        old.setDescription2(cdcHospital.getDescription2());
        old.setDataId(cdcHospital.getDataId());
        old.setIsActive(cdcHospital.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
